<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class star_db extends Model{
	public $pk = 'star_id';
	public $table = 'ptx_staruser';
	var $linker = array(
			'user'=>array(
					'type' => 'hasone',
					'map' => 'user',
					'mapkey' => 'user_id',
					'ftable' => 'ptx_user',
					'fkey' => 'user_id',
					'enabled' => true
			),
			'category'=>array(
					'type' => 'hasone',
					'map' => 'category',
					'mapkey' => 'category_id',
					'ftable' => 'ptx_staruser_category',
					'fkey' => 'category_id',
					'enabled' => true
			)
	);

	var $select_fields = " ptx_staruser.*,user.user_id,user.email,user.nickname,user.user_title,user.bio,user.total_like,user.total_share,category.* ";

	private function init_conditions($conditions){
		$conditions_user = NULL;
		if(isset($conditions['keyword'])){
			$keyword = $this->escape($conditions['keyword']);
			$conditions_user .= "AND MATCH (user.nickname) AGAINST ('{$keyword}' IN BOOLEAN MODE) OR MATCH (user.email) AGAINST ('{$keyword}' IN BOOLEAN MODE) ";
		}
		if(isset($conditions['category_id'])){
			$category_id = $this->escape($conditions['category_id']);
			$conditions_user .= "AND ptx_staruser.category_id={$category_id} ";
		}
		if(strpos($conditions_user, 'AND') === 0){
			$conditions_user = substr($conditions_user, 3);
		}
		return $conditions_user;
	}

	public function search($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_staruser.display_order DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}

	public function search_no_page($conditions=NULL,$fields = null,$sort=null,$limit){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_staruser.display_order DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->findAllJoin($conditions,$sort,$fields,$limit);
	}

	public function getStarById($starid){
		return $this->find_one(array('star_id'=>$starid));
	}

	public function deleteStarById($starid){
		return $this->deleteBy('star_id', $starid);
	}
	public function updateStarById($starid,$data){
		return $this->update(array('star_id'=>$starid), $data);
	}

	public function find_one($conditions=NULL){
		$fields = $this->select_fields;
		return $this->findJoin($conditions,null,$fields);
	}

	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			$id = $this->create($data);
			return $id;
		}
		return false;
	}

	public function check_exits($user_id){
		return $this->find(array('user_id'=>$user_id))?true:false;
	}

	private function check_value($data){
		if(!is_numeric($data['user_id'])||!is_numeric($data['category_id'])){
			return false;
		}
		return true;
	}

	public function change_category($condition,$categoryid){
		return $this->update($condition, array('category_id'=>$categoryid));
	}

	public function get_staruser_cache($time=1000){
		$results = pcache('r','staruser_cache');
		if(!$results){
			return $this->update_staruser_cache($time);
		}
		return $results;
	}

	public function update_staruser_cache($time=1000){
		$starcategory = load_db('star/starcategory');
		$staruser_categoty = array();
		$categories = $starcategory->findAll();
		foreach($categories as $cate){
			$key = 'scat_'.$cate['category_id'];
			$staruser_categoty[$key]['category'] = $cate;
			$conditions['category_id'] = $cate['category_id'];
			$starusers = $this->search_no_page($conditions,null, ' user.total_like DESC,ptx_staruser.display_order DESC ',10);
			$staruser_categoty[$key]['staruser'] = $starusers;
		}
		pcache('w','staruser_cache',$staruser_categoty,$time);
		return $staruser_categoty;
	}

}
